package com.tjjingpan.databasetest

import android.content.ContentProvider
import android.content.ContentValues
import android.content.UriMatcher
import android.database.Cursor
import android.net.Uri

class DataBaseProvider : ContentProvider() {
    private val bookDir = 0 //Book表中的所有数据
    private val bookItem = 1//Book表中的单条数据
    private val categoryDir = 2//Category表中的所有数据
    private val categoryItem = 3//Category表中的单条数据
    private val authority = "com.example.databasetest.provider"
    private var dbHelper:MyDatabaseHelper ?= null

    private val uriMatcher by lazy {
        val matcher = UriMatcher(UriMatcher.NO_MATCH)
        matcher.addURI(authority,"book",bookDir)
        matcher.addURI(authority,"book/#",bookItem)
        matcher.addURI(authority,"category",categoryDir)
        matcher.addURI(authority,"category/#",categoryItem)
        matcher
    }

    override fun onCreate() = context?.let {
        dbHelper = MyDatabaseHelper(it,"BookStore.db",2)
        true
    }?:false

    override fun query(uri: Uri, projection: Array<String>?, selection: String?,
                       selectionArgs: Array<String>?, sortOrder: String?) = dbHelper?.let {
        //查询数据
        val db = it.readableDatabase
        val cursor = when(uriMatcher.match(uri)){
            bookDir -> db.query("Book",projection,selection,selectionArgs,null,null,sortOrder)
            bookItem -> {
                val bookId = uri.pathSegments[1]
                db.query("Book",projection,"id = ?", arrayOf(bookId),null,null,sortOrder)
            }
            categoryDir -> db.query("Category",projection,selection,selectionArgs,null,null,sortOrder)
            categoryItem -> {
                val categoryId = uri.pathSegments[1]
                db.query("Category",projection,"id = ?", arrayOf(categoryId),null,null,sortOrder)
            }
            else ->null
        }
        cursor
    }


    override fun insert(uri: Uri, values: ContentValues?) = dbHelper?.let {
        //添加数据
        val db = it.writableDatabase
        val uriReturn = when(uriMatcher.match(uri)){
            bookDir,bookItem ->{
                val newBookId = db.insert("Book",null,values)
                Uri.parse("content://$authority/book/$newBookId")
            }
            categoryDir,categoryItem ->{
                val newCategoryId = db.insert("Category",null,values)
                Uri.parse("content://$authority/category/$newCategoryId")
            }
            else -> null
        }
        uriReturn
    }

    override fun update(uri: Uri, values: ContentValues?, selection: String?,
        selectionArgs: Array<String>?) = dbHelper?.let {
        //更新数据
        val db = it.writableDatabase
            val updatedRows = when(uriMatcher.match(uri)){
                bookDir -> db.update("Book",values,selection,selectionArgs)
                bookItem -> {
                    val bookId = uri.pathSegments[1]
                    db.update("Book",values,"id = ?", arrayOf(bookId))
                }
                categoryDir -> db.update("Category",values,selection,selectionArgs)
                categoryItem -> {
                    val categoryId = uri.pathSegments[1]
                    db.update("Category",values,"id = ?", arrayOf(categoryId))
                }
                else -> null
            }
        updatedRows

    } ?:0

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?) = dbHelper?.let {
        //删除数据
        val db = it.writableDatabase
        val deleteRows = when(uriMatcher.match(uri)){
            bookDir -> db.delete("Book",selection,selectionArgs)
            bookItem ->{
                val bookId = uri.pathSegments[1]
                db.delete("Book","id = ?", arrayOf(bookId))
            }
            categoryDir -> db.delete("Category",selection,selectionArgs)
            categoryItem ->{
                val categoryId = uri.pathSegments[1]
                db.delete("Category","id = ?", arrayOf(categoryId))
            }
            else -> null
        }
        deleteRows
    }?: 0

    override fun getType(uri: Uri)= when(uriMatcher.match(uri)){
        bookDir -> "vnd.android.cursor.dir/vnd.com.example.databasetest.provider.book"
        bookItem -> "vnd.android.cursor.item/vnd.com.example.databasetest.provider.book"
        categoryDir ->"vnd.android.cursor.dir/vnd.com.example.databasetest.provider.category"
        categoryDir ->"vnd.android.cursor.item/vnd.com.example.databasetest.provider.category"
        else -> null
    }
}